'use strict';

const Controller = require('egg').Controller;

// 文件存储

const fs = require('fs');

const path = require('path');
const pump = require('mz-modules/pump');
// const awaitWriteStream = require('await-stream-ready').write;

// const sendToWormhole = require('stream-wormhole');

class FileController extends Controller {
    async uploadImg() {
        const { ctx } = this;
        const file = ctx.request.files[0];
        if (!file) return ctx.throw(404);

        const filename = file.filename;
        const targetPath = path.join(this.config.baseDir, 'app/asserts/img', filename);
        const source = await fs.createReadStream(file.filepath);
        const target = await fs.createWriteStream(targetPath);
        await pump(source, target);
        ctx.body = { url: '/img/' + filename };
    }
}

module.exports = FileController;
